<template>
	<view class="">
		<common-title :title="titleContent" @clickLeft="backToUpPage" @clickRight="editUserSex"></common-title>
		<view class="user-sex-view">
			<radio-group class="user-sex-radio" @change="editSex">
				<label v-for="(item, index) in sexInfo" :key="index" class="user-sex-radio-item">
					<view>{{item.name}}</view>
					<radio :value="item.value" :checked="item.value === userSex" color="#18bc37" borderColor="#fff"></radio>
				</label>
			</radio-group>
		</view>
	</view>
</template>

<script setup>
	import { ref } from "vue";
	import { onLoad } from '@dcloudio/uni-app'
	import { editUserInfo } from "../../../../utils/apiMine";
	//================================common-title=========================================
	//标题内容
	const titleContent = ref({
		left:'',
		midst:'修改性别',
		right:'保存'
	});
	
	//返回
	const backToUpPage = () => {
		uni.navigateBack();
	};
	//保存修改
	const editUserSex = async () => {
		await editUserInfo({'sex': userSex.value}).then((value) => {
			// console.log(value==='200');
			if (value === '200') {
				uni.showToast({
					title:'保存成功'
				})
			}
		}).finally(() => {
			uni.redirectTo({
				url:'/pages/mine/other_set/personal-set'
			});
		})
	}
	//---------------------------------修改性别---------------------------------------------
	const sexInfo = [
		{
			value:'1',
			name:'男'
		},
		{
			value:'0',
			name:'女'
		}
	];
	const userSex = ref(null);
	onLoad((options) => {
		// 从 options 中获取 userSex
		userSex.value = options.sex ? decodeURIComponent(options.sex) : '';
		// console.log(userSex.value);
	});
	const editSex = (e) => {
		// console.log(e);
		userSex.value = e.detail.value;
	}
</script>

<style lang="scss" scoped>
	.user-sex-view{
		display: flex;
		justify-content: center;
		align-items: center;
		.user-sex-radio{
			display: flex;
			flex-direction: column;
			width: 690rpx;
			.user-sex-radio-item{
				display: flex;
				justify-content: space-between;
				padding: 20rpx;
				border-bottom: 1px solid #e5e5e5;
				font-size: 36rpx;
			}
		}
	}
</style>